package com.angel.web.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;


@Controller
@RequestMapping("/app/demo")
public class DemoController{

	@RequestMapping("/index")
	public void index(HttpServletResponse response,HttpServletRequest request){
	        String path = request.getRealPath(""); //值为D:\apache-tomcat-6.0.26\webapps\createpdf  
	        //把路径中的反斜杠转成正斜杠  
	        path = path.replaceAll("\\\\", "/"); //值为D:/apache-tomcat-6.0.26/webapps/createpdf     
//	        String path2 = request.getRealPath("/");   
//	        String path3 = request.getContextPath();  
//	        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path3+"/";  
	        response.setContentType("application/pdf");  
	        response.setHeader("Content-Disposition", "inline; filename=WebReport.pdf");  
	          
	        StringBuffer html = new StringBuffer();  
	        //组装成符合W3C标准的html文件，否则不能正确解析  
//	        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
//	        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">")  
//	        .append("<head>")  
//	        .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")  
//	        .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")  
//	        .append("<style type=\"text/css\">img {width: 700px;}</style>")  
//	        .append("</head>")  
//	        .append("<body>");  
//	          
//	        html.append("<center><h1>统计报表11111111111111111</h1></center>");  
//	        html.append("<div style=\"border: 1px solid red;\">进伟个屌丝</div>");  
//	        html.append("<center>");  
//	        html.append("<img src=\"images/body2-bg.jpg\"/>");
//	        html.append("<img src=\"images/icon-01.png\"/>");
//	        html.append("</center>");  
//	          
//	        html.append("</body></html>");  
//	        html.append(read());
	        OutputStream os = null;
	        // parse our markup into an xml Document  
	        try {  
	            ITextRenderer renderer = new ITextRenderer();  
	            /** 
	             * 引入了新的jar包，不用再导入字体了 
	            */
	            ITextFontResolver fontResolver = renderer.getFontResolver(); 
	            /*fontResolver.addFont(null, null); */
	              
//	            renderer.setDocumentFromString(html.toString());  
	            renderer.setDocument(new File("e:\\test.html"));
	            // 解决图片的相对路径问题    
	            renderer.getSharedContext().setBaseURL("file:///" + path + "/images/");  
	            renderer.layout();  
//	            os = response.getOutputStream();  
	            os = new FileOutputStream("E:/a.pdf");
	            renderer.createPDF(os);
	            os.close();
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }
	}
	
	@RequestMapping("ddd")
	public String ddd(){
		return "/index";
	}
	
}
